<?php
namespace app\admin\controller;

use think\facade\View;
use app\Request;
use app\repositories\contracts\RootInterface;
use app\repositories\contracts\RootGroupInterface;

class Root extends ViewBaseController
{
    public function index()
    {
        //halt(md5('vee7z'.'yhw1230'));
        return View::fetch('index');
    }

    public function getRootListJson(Request $request,RootInterface $root)
    {
        $page = $request->param('page');
        $limit = $request->param('limit');
        return layuiJson(0,'管理员列表获取成功',$root->getRootListPage($page,$limit,'id','asc'),$root->getRootListCount());
    }

    /***
     * @param RootInterface $root
     * @return \think\response\Json
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 9:21
     * @Description 删除管理员接口
     */
    public function del(Request $request,RootInterface $root)
    {
        $idArray = $request->param('idArray');
        $bool = $root->delRootList($idArray);
        if(!$bool)
        {
            $result = layuiJson(1,'删除失败');
            return $result;
        }
        return layuiJson(0,'删除成功',$idArray);
    }

    /***
     * @param Request $request
     * @param RootInterface $root
     * @return \think\response\Json
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 10:40
     * @Description 修改指定字段接口
     */
    public function editList(Request $request,RootInterface $root)
    {
        $editArray = $request->param();
        //halt($editArray);
        $result = $root->editRootList($editArray);
        if(!$result)
        {
            return layuiJson(1,'修改失败');

        }
        return layuiJson(0,'修改成功',$result);
    }

    /***
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 13:46
     * @Description 添加管理员
     */
    public function add(Request $request,RootInterface $root,RootGroupInterface $group)
    {
        if(!$request->isPost()){
            $allRootGroupList = $group->getRootGroupList();
            return View::fetch('',['allRootGroupList'=>$allRootGroupList]);
        }

        $addArray = $request->param();
        $result = $root->addRootList($addArray);
        if(!$result){
            return layuiJson(1,'添加失败,用户名可能已存在',$result);
        }

        return layuiJson(0,'添加成功',$result);
    }

    /***
     * @return string
     * @Author Vee7
     * @Date 2021/12/22
     * @Time 15:04
     * @Description 修改头像iframe
     */
    public function editAvatar($id)
    {
        return View::fetch('editAvatar',['editId'=>$id]);
    }

    /***
     * @Author Vee7
     * @Date 2021/12/22
     * @Time 15:26
     * @Description 文件上传
     */
    public function upload($id,RootInterface $root)
    {
        // 获取表单上传文件
        $files = request()->file();
        try {
            validate(['image'=>'fileSize:10240|fileExt:jpg,jpeg,png,gif,bmp'])
                ->check($files);
            $savename = [];
            foreach($files as $file) {
                $savename[] = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file,'sha1');
            }
            //更新数据库(数据格式：)
            //^ array:1 [
            //  "editArray" => array:1 [
            //    0 => array:2 [
            //      "id" => "6"
            //      "group_id" => "ff"
            //    ]
            //  ]
            //]
            $showPath = '/storage/'.$savename[0];
            $editArray = [
                  "editArray" =>  [
                        ["id" => $id,"avatar_src" => $showPath]
                  ]
            ];
            $root->editRootList($editArray);

            return layuiJson(0,'上传成功!',$showPath);
        } catch (\think\exception\ValidateException $e) {
            return layuiJson(1,'上传失败，'.$e);
        }
    }
}
